<template>

<SearchFrom
:searchData ="pagination"
@handleSearch="handleSearch"
@handleReset="handleReset"
></SearchFrom>

  <TableList
  :data="data"
  :total="total"
  :pagination="pagination"
  @onPageChange="onPageChange"
  @handleBulid="handleBulid"
  @edit="edit"
  @deleteById="deleteById"
  @tableSOS="tableSOS"
  ></TableList>

  <DialogForm
   :visible="visible"
   @handleClose="handleClose"
   @handleAdd="handleAdd"
   ref="Clear"
   :title="title"
   :data="fromBaseData"
   @handleEdit="handleEdit"
  ></DialogForm>

  <index
  :deleteid="deleteid"
  :visible="visibles"
  @deleteById="deleteById"
  @handleSubmit="handleSubmit"
  @handleCloseDelete="handleCloseDelete"
  ></index>

  <startOrStop
  :sosId="sosId"
  :test="test"
  :visible="visibleSOS"
  @handleSubmitSOS="handleSubmitSOS"
  @handleCloseSOS="handleCloseSOS"
  ></startOrStop>

</template>

<script setup lang="jsx">
import { ref,onMounted } from 'vue';
import {projectStatus,getProjectList , projectAdd,getProjectDetails,projectUpdate,projectDelete} from '@/api/serve'
import TableList from './components/TableList.vue';
import SearchFrom from './components/SearchForm.vue';
import DialogForm from './components/DialogForm.vue';
import { MessagePlugin } from 'tdesign-vue-next';
import index from '@/components/OperateDialog/index.vue'
import startOrStop from '@/components/Forbidden/index.vue'

const visible=ref(false)
const data = ref([]);
const total = ref(0);
const title=ref()
const fromBaseData=ref({})
const visibles=ref(false)
let deleteid=0

const typeId=ref('')
const typeStauts=ref('')
const stautsText=ref('')
const visibleSOS=ref(false)
const test=ref()
let sosId=0


const pagination = ref({
  pageSize: 10,
  pageNum: 1
})
//生命周期
onMounted(() => {
  getList()
})

const Clear=ref({})

//调用接口
const getList = async () => {
  const res = await getProjectList(pagination.value)
  data.value = res.data.records
  total.value = Number(res.data.total)
}

// 翻页设置当前页
const onPageChange = (val) => {
  pagination.value.pageNum = val.current
  pagination.value.pageSize = val.pageSize
  getList()
}


const handleSearch = () => {
  getList()
}

// 重置
const handleReset=()=>{
  pagination.value = {
    pageNum: 1,
    pageSize: 10
  }
    getList() 
  }

//打开弹窗
const handleBulid=()=>{
  title.value='新增'
  Clear.value.handleClear()
  visible.value=true
}

//关闭弹窗
const handleClose=()=>{
  visible.value=false
}

//新增护理项目
const handleAdd=async(val)=>{
  const res=await projectAdd(val)
  if(res.code===200){
    //提示 添加成功
    MessagePlugin.success('添加成功')
    handleClose()
    getList()
  }
}


//编辑的弹窗
const edit=(val)=>{
  title.value='编辑'
  visible.value=true
  //数据回显
  handleedit(val)
}

//编辑弹窗回显回显
const handleedit=async(val)=>{
  const res=await getProjectDetails(val)
  if(res.code===200){
    fromBaseData.value=res.data
  }
}

//提交编辑数据
const handleEdit=async(val)=>{
  const res=await projectUpdate(val)
  if(res.code===200){
    MessagePlugin.success('编辑成功')
    handleClose()
    getList()
  }
}

//删除确认弹框弹出
const deleteById=(id)=>{
  deleteid=id
  visibles.value=true
}

//根据id删除
const handleSubmit=async(id)=>{
  const res=await projectDelete(id)
  if(res.code===200){
    MessagePlugin.success('删除成功')
    getList()
    handleCloseDelete()
  }
}

//关闭删除弹窗
const handleCloseDelete=()=>{
  visibles.value=false
}

//打开禁用弹窗
const tableSOS=(row)=>{
  debugger
  typeId.value=row.id
  if(row.status===1){
    typeStauts.value=0
    stautsText.value='禁用成功'
    debugger 
     visibleSOS.value=true
  }else{
    typeStauts.value=1
    debugger
    handleSubmitSOS()
        stautsText.value='启动成功'
  }

}

//提交禁用启用
const handleSubmitSOS=async()=>{

  const param={
    id:typeId.value,
    status:typeStauts.value
  }
  const res=await projectStatus(param)
  if(res.code===200){
    MessagePlugin.success(stautsText.value)
    getList()
    handleCloseSOS()
  }
}

const handleCloseSOS=()=>{
  visibleSOS.value=false
}

</script>